<template>
  <div class="login-page">
    <el-card class="el-card">
      <template #header>
        <span>黑马面经运营后台</span>
      </template>
      <div>
        <el-form ref="formRef" :model='form' :rules='rules'>
          <!-- 输入框 -->
           <el-form-item label="用户名:" prop='username'>
             <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
           </el-form-item>
           <el-form-item label="密码:" prop='password'>
             <el-input v-model="form.password" placeholder="请输入密码" type="password"></el-input>
           </el-form-item>
           <!--按钮 -->
           <el-form-item class="tc">
             <el-button type="primary" @click="login">登 录</el-button>
             <el-button @click="reset">重 置</el-button>
           </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { login } from '@/api/user'
// import {setToken} from '@/store/modules'
export default {
  name: 'LoginIndex',
  data(){
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          /*必填校验*/
          { required: true, message: '请输入用户名', trigger: ['blur','change'] },
          { pattern: /^\w{5,11}$/, message: '请输入5-11位的任意字符', trigger: ['blur','change']}
        ], 
        password: [
          { required: true, message: '请输入密码', trigger: ['blur','change'] },
          { pattern: /^\w{5,11}$/, message: '请输入5-11位的密码', trigger: ['blur','change']}
        ]
      }
    }
  },
  methods:{
    login(){
      this.$refs.formRef.validate( async(valid)=>{
        if(valid) {
         const res = await login( this.form )
            console.log(res);
            this.$store.commit('user/setToken',res.data.data.token)
            // setToken(res.data,data.token)
            this.$message.success('登陆成功')
            this.$router.push('/')
        }
      })
    },
    reset(){
      this.$refs.formRef.resetFields()
    }
 
  }
}
</script>

<style lang="scss" scoped>
.login-page{
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  // 利用 flex 控制水平垂直居中
  display: flex;
  align-items: center;
  justify-content: space-around;

  .el-card{
    width: 420px;

     ::v-deep .el-card__header{
      height: 80px;
      background: rgba(114,124,245,1);
      color: #fff;
      text-align: center;
      line-height: 40px;
      font-size: 18px;
     }
  }
  .el-form {
    padding: 0 20px;
  }
   .tc {
    text-align: center;
  }
}
</style>

